//
//  ReportController.m
//  HarmonizePub
//
//  Created by rzhy on 16/8/24.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "ReportController.h"
#import "Config.h"
#import "ReportTableViewCell.h"
#import "CheckRepot.h"
#import "ExamineReport.h"
#import "MJExtension.h"
#import "ReportDetailViewController.h"
#import "Patient.h"

static NSString * const CellIdentifier = @"Cell";

@interface ReportController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *jcbgArray; // 检查报告
@property (nonatomic, strong) NSArray *jybgArray; // 检验报告
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation ReportController
- (NSArray *)jcbgArray
{
    if (_jcbgArray == nil) {
        _jcbgArray = [[NSArray alloc] init];
    }
    return _jcbgArray;
}
- (NSArray *)jybgArray
{
    if (_jybgArray == nil) {
        _jybgArray = [[NSArray alloc] init];
    }
    return _jybgArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"取报告单";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    UINib *nib = [UINib nibWithNibName:[ReportTableViewCell registerName] bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];

    // 加载数据, 默认选择第一页
    [self setupData];
}

- (IBAction)selectSegment:(UISegmentedControl *)sender {
    self.selectIndex = sender.selectedSegmentIndex;
    [self.tableView reloadData];
}

- (void)setupData
{
    
    Patient *patient = [Patient sharedPatient];
    // 加载tableView数据
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    NSDictionary *params = @{@"id" : @(patient.id)};
    // 检查报告
    [HTTPSTool POSTRequestWithPath:XHJcbglb cookie: cookie params: params success:^(NSDictionary * _Nonnull JSON) {
        LRLog(@"%@", JSON);
        // 字典数组转模型数组
        self.jcbgArray =[CheckRepot mj_objectArrayWithKeyValuesArray:[JSON valueForKey:@"data"][@"list"]];
    }];
    // 检验报告
    [HTTPSTool POSTRequestWithPath:XHJybglb cookie: cookie params: params success:^(NSDictionary * _Nonnull JSON) {
        // 字典数组转模型数组
        self.jybgArray = [ExamineReport mj_objectArrayWithKeyValuesArray:[JSON valueForKey:@"data"][@"list"]];
//        LRLog(@"JSON:%@",[JSON valueForKey:@"data"][@"list"]);
        [self.tableView reloadData];
    }];
}

#pragma mark - tableViewDataSource/tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.selectIndex == 0) {
        return  self.jybgArray.count;
    } else {
        return  self.jcbgArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // 根据模型设置Cell内容
    if (self.selectIndex == 0) {
        ExamineReport *examineReport = self.jybgArray[indexPath.row];
        cell.examineReport = examineReport;
        
    } else {
        CheckRepot *checkRepot = self.jcbgArray[indexPath.row];
        cell.checkRepot = checkRepot;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (self.selectIndex == 0) { // 检验报告详情
        ExamineReport *examineReport = self.jybgArray[indexPath.row];
        // 跳转到明细列表
        [self toReportDetailViewControllerWith: nil and: examineReport];
    } else { // 检查报告详情
        CheckRepot *checkRepot = self.jcbgArray[indexPath.row];
        // 跳转到明细列表
        [self toReportDetailViewControllerWith: checkRepot and: nil];
    } 
}

// 跳转到明细列表
- (void)toReportDetailViewControllerWith: (CheckRepot *)checkRepot and: (ExamineReport *)examineReport
{
    ReportDetailViewController *checkController = [[ReportDetailViewController alloc] init];
    if (checkRepot) {
        checkController.Id = checkRepot.jcdh;
        checkController.name = checkRepot.jcxm;
        checkController.type = 0;
    }else{
        checkController.Id = examineReport.jydh;
        checkController.name = examineReport.jymc;
        checkController.type = 1;
    }
    checkController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:checkController animated:YES];
}

@end
